#!/usr/bin/python
# -*- coding: UTF-8 -*-
import json
import os
import sys
reload(sys)
sys.setdefaultencoding('utf8')

configJsonFile = '../tools/excelsjsons/CustomersConfig.json'


def _getDataFromFile(filePath):
    print("filePath  is ",filePath)
    with open(filePath, 'r') as f:
        fileData = f.read()
        f.close()
        return fileData


def _saveDataToFile(filePath, data):
    with open(filePath, 'w') as f:
        f.write(data)
        f.close()


def getJsonDataFromFile(filePath):
    fileStr = _getDataFromFile(filePath)
    fileData = None
    if (fileStr):
        fileData = json.loads(fileStr)
    return fileData


def saveJsonDataToFile(filePath, data):
    dataStr = json.dumps(data)
    _saveDataToFile(filePath, dataStr)


def loadConfigData():
    global configData
    configJsonRealPath = os.path.join(os.getcwd(), configJsonFile)
    configJsonRealPath = os.path.abspath(configJsonRealPath)
    configData = getJsonDataFromFile(configJsonRealPath)
    print(configData)
    print('load config file end!')


def getCurConfig():
    global curConfigData
    customerName = os.environ['CustomerName']
    print('curent customer = ', customerName)
    for key in configData:
        if key == customerName:
            curConfigData = configData[key]
            print('curConfigData =', curConfigData)


def getGitHubUrlByNetType(netType):
    gitHubUrl = netType + '_gitHubUrl'
    print('getGitHubUrlByNetType =', gitHubUrl)
    if not curConfigData:
        getCurConfig()
    print('curConfigData[gitHubUrl] =' + str(curConfigData[gitHubUrl]))
    return str(curConfigData[gitHubUrl])


def getCustomerName():
    return os.environ['CustomerName']


def getBigVersion():
    if not curConfigData:
        getCurConfig()
    bigVersion = curConfigData['bigVersion']
    print('bigVersion =', bigVersion)
    return bigVersion


def getTemplateBranch():
    if not curConfigData:
        getCurConfig()
    templateBranch = curConfigData['templateBranch']
    print('templateBranch =', templateBranch)
    return templateBranch


def getZipUpdateUrlByNetType(netType):
    zipUpdateUrl = netType + '_zipUpdateUrl'
    print('getZipUpdateUrlByNetType =', zipUpdateUrl)
    if not curConfigData:
        getCurConfig()
    print('curConfigData[zipUpdateUrl] =' + curConfigData[zipUpdateUrl])
    return curConfigData[zipUpdateUrl]
